package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.ReservationDAO;
import com.foreveross.util.DateUtil;
import com.foreveross.vo.OrderService;

@Component("reservationDAO")
public class ReservationDAOImp extends SQLUtil implements ReservationDAO {
	@Override
	public List<OrderService> findReservationList(String weixinId,
			int pageIndex, int pageSize, String isweixin,String name,String type,String phone,String houseId,String status,String userId,String resCode)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from OrderService model where 1=1 ");
		if (!"".equals(weixinId) && null != weixinId) {
			queryString
					.append(" and model.userId.wechatId ='" + weixinId + "'");
		}
		if (!"".equals(name) && null != name) {
			queryString
					.append(" and model.userName like '%" + name + "%'");
		}
		if (!"".equals(type) && null != type) {
			queryString
					.append(" and model.orderType.codeId ='" + type + "'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString
					.append(" and model.phone like '%" + phone + "%'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString
					.append(" and model.userId.hrHouses.id in ('" + houseId + "')");
		}
		if (!"".equals(status) && null != status) {
			queryString
					.append(" and model.status='" + status + "'");
		}
		if (!"".equals(resCode) && null !=resCode) {
			queryString
					.append(" and model.userId.resCode like '%" + resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseId.id");
		queryString.append(" order by model.id desc");
		
		return (List<OrderService>)findObjectAll(queryString.toString(), pageIndex,
				pageSize);
	}
	

	@Override
	public long findReservationListCount(String weixinId, String isweixin,
			String name, String type, String phone, String houseId,
			String status, String userId,String resCode) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from OrderService model where 1=1 ");
		if (!"".equals(weixinId) && null != weixinId) {
			queryString
					.append(" and model.userId.wechatId ='" + weixinId + "'");
		}
		if (!"".equals(name) && null != name) {
			queryString
					.append(" and model.userName like '%" + name + "%'");
		}
		if (!"".equals(type) && null != type) {
			queryString
					.append(" and model.orderType.codeId ='" + type + "'");
		}
		if (!"".equals(phone) && null != phone) {
			queryString
					.append(" and model.phone like '%" + phone + "%'");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString
					.append(" and model.userId.hrHouses.id in ('" + houseId + "')");
		}
		if (!"".equals(status) && null != status) {
			queryString
					.append(" and model.status='" + status + "'");
		}
		if (!"".equals(resCode) && null !=resCode) {
			queryString
					.append(" and model.userId.resCode like '%" + resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseId.id");
		queryString.append(" order by model.id desc");		
		return (Long)findObjectAll(queryString.toString()).get(0);
	}



	@Override
	public void deleteReservation(String id) throws Exception {
		final String sql = "delete from OrderService t where t.id='" + id + "'";
		updateObejctByHql(sql);
	}

	@Override
	public void updateStatusAndOrderTimeById(OrderService orderService) throws Exception {
		String sql="update OrderService set status='"+orderService.getStatus()+"',orderTime='"+DateUtil.DateToString(orderService.getOrderTime(), "yyyy-MM-dd HH:mm:ss")+"' where id='"+orderService.getId()+"'";
		updateObejctByHql(sql);
	}

}
